import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

import 'all_reply.dart';
import 'no_reply.dart';

///
/// 顾客评价
///
class CustomerEvaluate extends StatefulWidget {
  const CustomerEvaluate({Key? key}) : super(key: key);

  @override
  State<CustomerEvaluate> createState() => _CustomerEvaluateState();
}

class _CustomerEvaluateState extends State<CustomerEvaluate> with SingleTickerProviderStateMixin {
  late TabController _controller;

  List<String> tabs = ['未回复的评价', '全部评价'];

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: tabs.length, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return customerEvaluate();
  }

  Widget customerEvaluate() {
    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return buildHeadWidget(context);
      },
      body: TabBarView(
        controller: _controller,
        children: const [NoReply(), AllReply()],
      ),
    );
  }

  Widget topContainer() {
    return Container(
      height: 55,
      margin: const EdgeInsets.only(bottom: Sizes.default10),
      decoration: const BoxDecoration(color: Colors.white),
      padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
      child: Row(
        children: [
          Align(
            alignment: Alignment.centerLeft,
            child: Text(
              '4.5',
              style: StylesUtils.customTextStyle(
                  color: ColorsUtils.colorFf0, fontSize: Sizes.default25),
            ),
          ),
          Expanded(
              child: Container(
            padding: const EdgeInsets.symmetric(horizontal: Sizes.default15),
            child: Column(
              children: [
                Expanded(
                    child: Container(
                  alignment: Alignment.centerLeft,
                  child: const BrnRatingStar(
                    selectedCount: 4,
                    count: 5,
                    space: 5,
                    canRatingZero: true,
                  ),
                )),
                Expanded(
                    child: Container(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    '根据最近90天评价计算所得',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default12, color: ColorsUtils.color515),
                  ),
                ))
              ],
            ),
          )),
          Align(
              alignment: Alignment.centerLeft,
              child: TextButton(
                onPressed: () {
                  Get.toNamed(RouteGet.evaluateStatistics);
                },
                child: Text.rich(TextSpan(
                    text: '更多',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default12, color: ColorsUtils.color797),
                    children: [
                      WidgetSpan(
                          child: BrunoTools.getAssetSizeImage(
                              BrnAsset.iconRightArrow, Sizes.default13, Sizes.default13),
                          alignment: PlaceholderAlignment.middle)
                    ])),
              ))
        ],
      ),
    );
  }

  List<Widget> buildHeadWidget(BuildContext context) {
    return [
      SliverToBoxAdapter(
        child: topContainer(),
      ),
      SliverPersistentHeader(
        pinned: true,
        delegate: CommonSilverAppBarDelegate(
            child: Container(
          height: Sizes.default50,
          alignment: Alignment.center,
          decoration: const BoxDecoration(color: Colors.white),
          child: ToolsUtils.getClientTabBar(tabs, _controller),
        )),
      ),
    ];
  }
}

class CommonSilverAppBarDelegate extends SliverPersistentHeaderDelegate {
  final Widget child;

  CommonSilverAppBarDelegate({required this.child});

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return child;
  }

  @override
  double get maxExtent => 50;

  @override
  double get minExtent => 40;

  @override
  bool shouldRebuild(covariant SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
